﻿using MyCanFD;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;

namespace CarPCBATest.Procedure
{
    public abstract class S12L_Led
    {
        private bool init = false;
        protected CanFdManager canFd;
        protected List<Thread> backgroundThreads = new List<Thread>();
        protected bool isLighting;
        protected int counter = 0; // 计数器
        public int StopLedLightness { get; set; }
        public int TurnLeftLedLightness { get; set; }
        public int TurnRightLedLightness { get; set; }

        public void Init()
        {
            if (init)
                return;
            canFd = new CanFdManager();
            canFd.Baud_Domain = 500000;
            canFd.Baud_Data = 2000000;
            canFd.QueueSize = 1024;
            canFd.OpenDevice();
            canFd.InitializeAndStart();

            init = true;
        }

        public int ReadReceivedQueue(out uint Id, out byte[] data)
        {
            int count = 0;
            Id = 0;
            data = null;
            if (canFd.ReceivedDataQueue.Count > 0)
            {
                count = canFd.ReceivedDataQueue.Count;
                var tmp = canFd.ReceivedDataQueue.Dequeue();
                Id = tmp.ID;
                data = tmp.Data;
            }

            canFd.ClearBuffer(0);
            return count;
        }

        public void StartSendCmd()
        {
            if (isLighting)
            {
                return;
            }
            counter = 0;
            isLighting = true;
            Stopwatch stopwatch1 = new Stopwatch();
            stopwatch1.Start();
            Thread threadCH1 = new Thread(() =>
            {
                while (isLighting)
                {
                    while (stopwatch1.ElapsedMilliseconds < 20)
                    {
                    }
                    SendControlLedCommand();
                }
            });
            threadCH1.Start();
            backgroundThreads.Add(threadCH1);
        }

        public void StopSendCmd()
        {
            if (!isLighting)
            {
                return;
            }
            isLighting = false;
            backgroundThreads.ForEach(th => th.Join());
        }

        public void Close()
        {
            if (init)
            {
                init = false;
                canFd.CloseDevice();
            }
        }

        protected abstract void SendControlLedCommand();

        //{
        //if (counter % 2 == 0)
        //{
        //    canFd.SendMessage(0x276, "00 00 00 00 00 00 00 00", 0);
        //}

        //if (counter % 2 == 1)
        //{
        //    byte stopLedLightness = Convert.ToByte(StopLedLightness);
        //    byte turnLeftLedLightness = Convert.ToByte(TurnLeftLedLightness);
        //    byte turnRightLedLightness = Convert.ToByte(TurnRightLedLightness);

        //    byte[] cmd0x277 = { stopLedLightness };
        //    cmd0x277 = cmd0x277.Concat(Enumerable.Repeat(turnLeftLedLightness, 15)).Concat(Enumerable.Repeat((byte)0, 48)).ToArray();

        //    byte[] cmd0x279 = { stopLedLightness };
        //    cmd0x279 = cmd0x279.Concat(Enumerable.Repeat(turnLeftLedLightness, 15)).Concat(Enumerable.Repeat((byte)0, 48)).ToArray();

        //    canFd.SendMessage(0x277, cmd0x277, 0);
        //    canFd.SendMessage(0x279, cmd0x279, 0);                                                                                                                                                                                                    //canFd.SendMessage(0x277, "20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ", 0);
        //}
        //counter++;
        //}
    }
}